//
//  AddPhone.m
//  Ui07_封装相册
//
//  Created by 徐会玲 on 15/5/13.
//  Copyright (c) 2015年 徐会玲. All rights reserved.
//

#import "AddPhone.h"

// 全局变量接受被改变大小的scrollView的tag值
NSInteger x;

@implementation AddPhone

//初始化
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 大的scroll
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_scrollView];
        _scrollView.backgroundColor = [UIColor grayColor];
        _scrollView.center = self.center;
        [_scrollView release];
        
        //设置属性
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
      
        
        self.arr = [NSMutableArray array];
        _scrollView.contentOffset = CGPointMake(frame.size.width, 0);
        
        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(60, 520, 250, 30)];
        _page.backgroundColor = [UIColor blackColor];
        [self addSubview:_page];
        [_page release];
        
        //添加方法
        [_page addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
        
        
    }
    return self;
}

-(void)addPicture:(NSMutableArray *)array
{
    
    // 滚动范围
    _scrollView.contentSize = CGSizeMake(([array count] + 2) * _scrollView.frame.size.width, 0);
    
    // 插入两张图片
    [_arr addObjectsFromArray:array];
    [_arr insertObject:[array lastObject] atIndex:0];
    [_arr insertObject:_arr[1] atIndex:[_arr count]];

    //  设置页数
    NSInteger v = [_arr count];
    _page.numberOfPages = v - 2;

    // 创建小scrollView
    for (int i = 0; i < [_arr count]; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width * i, 70, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        [_scrollView addSubview:scroll];
        
        
        
        [scroll release];
        
        scroll.delegate = self;
        scroll.minimumZoomScale = 0.25;
        scroll.maximumZoomScale = 2;
        scroll.tag = i + 5;
        
        // 添加照片
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_arr[i]]];
        image.frame = CGRectMake(0, 0, _scrollView.frame.size.width, 350);
        [scroll addSubview:image];
        [image release];
        
    }
    

    
}


// 缩放效果
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSLog(@"dfkdlkfldkf");
    x = scrollView.tag;
    return [scrollView.subviews firstObject];
    
}

// 减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    // 小圆点位置
    _page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    
    
    
    // 如果跳到别的照片  照片大小回到原来大小
    UIScrollView *ss = (UIScrollView *)[self.scrollView viewWithTag:x];
    if (scrollView.contentOffset.x != ss.contentOffset.x)
    {
        ss.zoomScale = 1;

    }
    
    // 最后一张照片就是第一张照片   所以改变偏移量
    if (scrollView.contentOffset.x == ([_arr count] - 1) * _scrollView.frame.size.width) {
        scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
        _page.currentPage = 0;
    }
    
    // 第一张照片是最后一张照片所以偏移量改变
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake((([_arr count] - 2) * _scrollView.frame.size.width), 0);
        _page.currentPage = [_arr count] - 2;
    }
    NSLog(@"%ld", x - 5);
}


// 让照片随着小圆点动
- (void)change:(UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * _scrollView.frame.size.width, 0)];
    
}


// 解决内存问题
-(void)dealloc
{
    
    [_arr release];
    [_scrollView release];
    [_page release];
    [super dealloc];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
